<template>
  <div>
    <!-- // 标签页 -->
    <van-tabs @click="onChannelChange">
      <van-tab
        v-for=" item in channelList"
        :key="item.id"
        :title="item.name"
        class="home-tabs"
      >
        <div class="search">
          <van-icon
            name="search"
            size="20"
            class="home-icon"
            @click="goSearch"
          />
          <van-icon
            name="filter-o"
            size="20"
            class="home-icon"
          />
        </div>
      </van-tab>
    </van-tabs>
    <!-- 主体 -->
    <main>
      <article-list :data="articleList" />
    </main>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex';
import moment from 'moment';
export default {
  name: 'HomePage',
  computed: {
    // 导入model层的数据
    ...mapState('channel', [
      'channelList'
    ]),
    ...mapState('article', [
      'articleList'
    ])
  },
  methods: {
    // 文章
    ...mapActions('channel', [
      'updateChannelList'
    ]),
    // 频道
    ...mapActions('article', [
      'updateArticleList'
    ]),
    getTime(timestamp) {
      return moment(timestamp).fromNow();
    },
    onChannelChange(id) {
      this.updateArticleList(id)
    },
    goSearch() {
      this.$router.push('/search')
    }
  },
  created() {
    this.updateChannelList()
    this.updateArticleList(0)
  }
}

</script>

<style lang="less" scoped>
@import url('@/assets/common.less');
.search{
  padding: 0px 5px;
  height: 44px;
  line-height: 44px;
  background-color: white;
  position: absolute;
  right: 0px;
  top: 0px;
  .home-icon{
    margin-right: 10px;
  }
}
main {
  height: calc(100% - 44px);
  overflow: auto;
  padding:  20px;
    padding: 0 8px;

    .article {
      padding: 8px 0;
      border-bottom: 1px solid #e9e9e9;

      &:last-child {
        border: none;
      }

      .article-type1 section {
        grid-template-columns: 2fr 1fr;
        gap: 16px;
      }

      .article-other section {
        grid-template-columns: 1fr 1fr 1fr;
        gap: 8px;
      }

      section {
        display: grid;
        margin: 8px 0;

        .img-wrapper {
          height: 82px;
          overflow: hidden;
          border-radius: 4px;

          img {
            height: 100%;
            width: 100%;
            // 图片适应父盒子的宽高
            object-fit: cover;
          }
        }
      }

      footer {
        .sub-text;

        span {
          margin-right: 8px;
        }
      }
    }
  }

</style>>
